package cn.bby.query.server.interfaces.rest;

import cn.bby.jtk.web.CommonResult;
import cn.bby.query.base.api.req.PushInterfaceInfoReq;
import cn.bby.query.base.model.req.DoQueryReq;
import cn.bby.query.base.model.req.QueryResponse;
import cn.bby.query.server.application.manager.DataQueryManager;
import cn.bby.query.server.application.manager.InterfaceInfoManager;
import cn.bby.query.server.application.manager.SystemStatusManager;
import cn.bby.query.server.domain.service.dto.DataChangeEvent;
import cn.bby.query.server.domain.service.update.DataUpdateHandleService;
import cn.bby.query.server.domain.share.constant.HealthInfoMetric;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ben
 * @since 2025/4/14 23:53
 */
@Slf4j
@RestController
@RequestMapping({""})
@RequiredArgsConstructor
public class BaseQueryController {
    private final InterfaceInfoManager interfaceInfoManager;
    private final DataQueryManager dataQueryManager;
    private final DataUpdateHandleService dataUpdateHandleService;
    private final SystemStatusManager systemStatusManager;

    /**
     * 查询
     */
    @PostMapping("/DoQuery")
    public CommonResult<QueryResponse<JSONObject>> doQuery(@RequestBody DoQueryReq req) {
        return CommonResult.success(dataQueryManager.doQuery(req));
    }

    /**
     * 汇报元数据
     */
    @PostMapping("/PostMetadata")
    public CommonResult<Void> postMetadata(@RequestBody PushInterfaceInfoReq req) {
        interfaceInfoManager.handleInterfaceInfos(req);
        return CommonResult.success();
    }

    /**
     * 更新数据
     */
    @PostMapping("/UpdateData")
    public CommonResult<Void> updateData(@RequestBody List<DataChangeEvent> events) {
        dataUpdateHandleService.handleUpdateDataEvent(events);
        return CommonResult.success();
    }

    /**
     * 更新数据
     */
    @RequestMapping("/HandleDelayEvent")
    public CommonResult<Void> updateData() {
        dataUpdateHandleService.handleDelayEvents();
        return CommonResult.success();
    }

    /**
     * 加载数据，仅用于测试
     */
    @RequestMapping("/LoadData")
    public CommonResult<Void> loadData() {
        interfaceInfoManager.loadInterfaceData();
        return CommonResult.success();
    }

    /**
     * 重新加载数据
     */
    @RequestMapping("/ReLoadData")
    public CommonResult<List<String> > reLoadData() {
        return CommonResult.success(interfaceInfoManager.reLoadInterfaceData());
    }

    /**
     * 心跳
     */
    @RequestMapping("/Heartbeat/{metric}")
    public CommonResult<Void> heartbeat(@PathVariable String metric) {
        systemStatusManager.heartbeat(HealthInfoMetric.valueOf(metric));
        return CommonResult.success();
    }

    /**
     * 获取系统状态
     */
    @RequestMapping("/IsHealth")
    public CommonResult<Boolean> isHealth() {
        return CommonResult.success(systemStatusManager.isHealth());
    }
}
